function [PQ, status, scheduledTargets] = IA_ScheduleRun_Hogge(PQ, ia_network, s1Targets, s2Targets)
%IA_ScheduleInit Summary of this function goes here
% Detailed explanation goes here

n = size(s1Targets, 2);
attemptedTargets(1:PQ_Size(PQ)) = NaN;
scheduledTargets(1:PQ_Size(PQ)) = NaN;
status = 0; %#ok<*NASGU>

k = 1;
for p = 1:PQ_Priorities(PQ)
  for t = 1:PQ_Size(PQ)
    % randomly select each task in the priority queue
    while 1
      attemptedTargets(t) = ceil(rand()*PQ_Size(PQ));
      been_done = 0;
      for s = 1:t-1
        if attemptedTargets(s) == attemptedTargets(t)
          been_done = 1;
          break;
        end
      end
      if ~been_done
        break;
      end
    end
    
    [Task, status] = PQ_Fetch_Task(PQ, attemptedTargets(t), PQ_Priorities(PQ)+1-p);
    if ~status
      continue;
    end
    
    i = ST_FindTargetId(s1Targets, Task.id);
    j = ST_FindTargetId(s2Targets, Task.id) + n;
    
    [ia_temp_net, consistent, ~] = IA_Network_Path_Consistency_Hogge(ia_network, i, j, IA_Relation_Equals());
    if consistent
      ia_network = ia_temp_net;
      scheduledTargets(k) = Task.id;
      k = k + 1;
    end
  end
end
scheduledTargets = scheduledTargets(1:k-1);
status = 1;

% Sort the scheduled targets by consulting the IA
for a = 1:size(scheduledTargets, 2)-1
  for b = a+1:size(scheduledTargets, 2)
    i1 = ST_FindTargetId(s1Targets, scheduledTargets(a));
    j1 = ST_FindTargetId(s1Targets, scheduledTargets(b));
    
    i2 = ST_FindTargetId(s2Targets, scheduledTargets(a)) + n;
    j2 = ST_FindTargetId(s2Targets, scheduledTargets(b)) + n;
    
    ia_relation1 = IA_Network_Fetch_Relation(ia_network, i1, j1);
    ia_relation2 = IA_Network_Fetch_Relation(ia_network, i2, j2);
    ia_relation = IA_Relation_Intersect(ia_relation1, ia_relation2);
    if IA_Relation_Is_Greater(ia_relation)
      temp = scheduledTargets(a);
      scheduledTargets(a) = scheduledTargets(b);
      scheduledTargets(b) = temp;
    end
  end
end
end